VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "EventLog"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

Private mstrUserName As String
Private mstrComputerName As String
Private mstrEventSource As String
Private mstrLogDestination As String
Private mbEnabled As Boolean

Private mobjFile As CFileOutputHandler
Public Property Get ComputerName() As String

  ComputerName = mstrComputerName

End Property

Friend Property Get OutputFile() As CFileOutputHandler
  
  If mbEnabled Then
    Set OutputFile = mobjFile
  End If

End Property

Public Property Get UserName() As String
  
  UserName = mstrUserName

End Property
Public Function WriteLog(Message As String, EventType As LogEventTypes, EventID As Long) As Boolean

  Dim objLine As DelimitedString

  If mbEnabled = False Then
    Err.Raise vbObjectError + ERR_WRITELOG_DISABLED, App.Title, "Can't write to log when Enabled = False"
    WriteLog = False
    Exit Function
  End If
  
  Set objLine = New DelimitedString
  
  With objLine
    .Add CStr(Now)
    .Add mstrEventSource
    .Add CStr(EventType)
    .Add Message
    .Add mstrUserName
    .Add mstrComputerName
  End With
  
  WriteLog = mobjFile.WriteLine(objLine.Value)
  
  Set objLine = Nothing

End Function


Public Property Let Enabled(ByVal NewEnabled As Boolean)

  If NewEnabled = mbEnabled Then
    Exit Property
  End If
  
  If mbEnabled Then
    mobjFile.CloseFile
    Set mobjFile = Nothing
    mbEnabled = False
  Else
    If Trim(mstrLogDestination) = "" Then
      Err.Raise vbObjectError + ERR_NO_LOGDEST, App.Title, "Can't set Enabled to True without a Log Destination"
      Exit Property
    End If
    If Trim(mstrEventSource) = "" Then
      Err.Raise vbObjectError + ERR_NO_SOURCE, App.Title, "Can't set Enabled to True without an Event Source"
      Exit Property
    End If
    Set mobjFile = New CFileOutputHandler
    If mobjFile.OpenFile(mstrLogDestination) = False Then
      Err.Raise vbObjectError + ERR_FILEOPEN_FAILED, App.Title, "Couldn't open file '" & mstrLogDestination & "'"
      Exit Property
    Else
      mbEnabled = True
    End If
  End If

End Property
Public Property Get Enabled() As Boolean

  Enabled = mbEnabled

End Property
Public Property Let LogDestination(ByVal NewLogDest As String)

  If mbEnabled Then
    Err.Raise vbObjectError + ERR_LOGDEST_DISABLED, App.Title, "Can't change LogDestination while Enabled property = True"
    Exit Property
  End If
  
  If Trim(NewLogDest) = "" Then
    Err.Raise vbObjectError + ERR_LOGDEST_NULL, App.Title, "LogDestination can't be a zero-length string"
    Exit Property
  End If

  mstrLogDestination = NewLogDest

End Property

Public Property Get LogDestination() As String

End Property

Public Property Let EventSource(ByVal NewSource As String)

  If mbEnabled Then
    Err.Raise vbObjectError + ERR_SOURCE_DISABLED, App.Title, "Can't set EventSource property while Enabled = True"
    Exit Property
  End If

  If Trim(NewSource) = "" Then
    Err.Raise vbObjectError + ERR_SOURCE_NULL, App.Title, "EventSource can't be a zero-length string"
    Exit Property
  End If

  mstrEventSource = NewSource

End Property
Public Property Get EventSource() As String

  EventSource = mstrEventSource

End Property

Private Sub Class_Initialize()

  Dim lpBuffer As String
  Dim nSize As Long
  Dim nRetVal As Long
  
  nSize = 255
  lpBuffer = Space(nSize)
  nRetVal = GetUserName(lpBuffer, nSize)
  mstrUserName = Left(lpBuffer, nSize - 1)
  
  nSize = 255
  lpBuffer = Space(nSize)
  nRetVal = GetComputerName(lpBuffer, nSize)
  mstrComputerName = Left(lpBuffer, nSize)

End Sub




